﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using QLKN.DataAccess;

namespace QLKN.GUI
{
    public partial class frmDangnhap : Form
    {
        public frmDangnhap()
        {
            InitializeComponent();
        }

        private void btnDangnhap_Click(object sender, EventArgs e)
        {
            if (KiemTraDangNhap(txtTendangnhap.Text, txtMatkhau.Text))
            {
                if (LayTrangThaiTaiKhoan(txtTendangnhap.Text).Equals("True"))
                {
                this.Hide();
                frmMain _frmMain = new frmMain(txtTendangnhap.Text);
                _frmMain.ShowDialog();
                this.Dispose();
                }
                else
                {
                    MessageBox.Show("Tài khoản không còn hoạt động", "THÔNG BÁO");
                    txtTendangnhap.Focus();
                    txtTendangnhap.SelectAll();
                }
            }
            else 
            {
                MessageBox.Show("Tên tài khoản và mật khẩu không trùng nhau!!!", "THÔNG BÁO");
                txtTendangnhap.Focus();
                txtTendangnhap.SelectAll();
            }
        }
        public string LayTrangThaiTaiKhoan(string tenDN)
        {
            SqlConnection conn = new SqlConnection();
            conn = Data.getConnection();
            string sql = @"SELECT nvtrangthai FROM nhanvien WHERE nvtendangnhap='" + tenDN + "'";
            conn.Open();
            DataTable dt = Data.getDataTable(sql, conn);
            string tt = dt.Rows[0]["nvtrangthai"].ToString();
            conn.Close();
            return tt;
        }
        public bool KiemTraDangNhap(string tendangnhap,string matkhau)
        {
            SqlConnection conn = Data.getConnection();
            string sql = "SELECT nvtendangnhap,nvmatkhau FROM nhanvien WHERE nvtendangnhap='"+tendangnhap+"' and nvmatkhau='"+matkhau+"'";
            SqlCommand cmd = new SqlCommand(sql,conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                conn.Close();
                return true;
            }
            conn.Close();
            return false;

        }

        private void frmDangnhap_Load(object sender, EventArgs e)
        {

        }
       

    }
}
